package com.cyjt.operation.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.sax.StartElementListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cyjt.operation.R;
import com.cyjt.operation.bean.StaticNodesStatusInfo;
import com.cyjt.operation.ui.StaticNodesItemActivity;

/**
 * 设备状态中静态节点列表的adapter
 * 
 * @author wsw
 * 
 */

public class StaticNodesStatusAdapter extends BaseAdapter {
	private Activity activity;
	private List<StaticNodesStatusInfo> nodesStatusInfolists;

	public StaticNodesStatusAdapter(Activity context,
			List<StaticNodesStatusInfo> nodesStatusInfolists) {
		this.activity = context;
		this.nodesStatusInfolists = nodesStatusInfolists;
	}

	@Override
	public int getCount() {
		return nodesStatusInfolists.size();
	}

	@Override
	public Object getItem(int position) {
		return nodesStatusInfolists.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public class ViewHolder {
		private TextView textView_item_for_parking_code,
				textView_item_for_parking_status,
				textView_item_for_nodes_areas, textView_item_for_nodes_status;
		private LinearLayout textView_item_for_left_linearLayout,
				textView_item_for_right_linearLayout;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = LayoutInflater.from(activity);
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = inflater.inflate(
					R.layout.list_view_item_for_static_nodes_status, null);
			viewHolder = new ViewHolder();
			viewHolder.textView_item_for_parking_code = (TextView) convertView
					.findViewById(R.id.textView_item_for_parking_code);
			viewHolder.textView_item_for_parking_status = (TextView) convertView
					.findViewById(R.id.textView_item_for_parking_status);
			viewHolder.textView_item_for_nodes_areas = (TextView) convertView
					.findViewById(R.id.textView_item_for_nodes_areas);
			viewHolder.textView_item_for_nodes_status = (TextView) convertView
					.findViewById(R.id.textView_item_for_nodes_status);
			viewHolder.textView_item_for_left_linearLayout = (LinearLayout) convertView
					.findViewById(R.id.textView_item_for_left_linearLayout);
			viewHolder.textView_item_for_right_linearLayout = (LinearLayout) convertView
					.findViewById(R.id.textView_item_for_right_linearLayout);
			convertView.setTag(viewHolder);
		} else
			viewHolder = (ViewHolder) convertView.getTag();
		final StaticNodesStatusInfo nodesStatusInfo = nodesStatusInfolists
				.get(position);
		if ("占用".equals(nodesStatusInfo.getStateToString())) {
			viewHolder.textView_item_for_parking_status.setTextColor(activity
					.getResources().getColor(R.color.red));
		} else if ("空闲".equals(nodesStatusInfo.getStateToString())) {
			viewHolder.textView_item_for_parking_status.setTextColor(activity
					.getResources().getColor(R.color.green));
		}
		viewHolder.textView_item_for_right_linearLayout
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						activity.startActivity(new Intent(activity,
								StaticNodesItemActivity.class).putExtra("code",
								nodesStatusInfo.getSensorCode()));
					}
				});
		viewHolder.textView_item_for_parking_code.setText(nodesStatusInfo
				.getCode() + "");
		viewHolder.textView_item_for_parking_status.setText(nodesStatusInfo
				.getStateToString() + "");
		viewHolder.textView_item_for_nodes_areas.setText(nodesStatusInfo
				.getTitle());
		viewHolder.textView_item_for_nodes_status.setText(nodesStatusInfo
				.getStatusToString());
		return convertView;
	}

}
